
local yuanjian = fk.CreateSkill {
  name = "lingling__yuanjian",
}

Fk:loadTranslationTable{
  ["lingling__yuanjian"] = "远见",
  [":lingling__yuanjian"] = "每轮限一次，你攻击范围内的其他角色出牌阶段开始时，你可以弃置一张牌，然后观看其所有手牌并暗中标记其中一张牌。"..
  "当其使用或打出此牌结算后，你摸两张牌并对其造成1点伤害。",

  ["#lingling__yuanjian-invoke"] = "远见：你可以弃置一张牌，观看 %dest 所有手牌并暗中标记其中一张牌",
  ["#lingling__yuanjian-ask"] = "远见：标记其中一张牌，当其使用或打出此牌后，你摸两张牌并对其造成伤害",
}

yuanjian:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yuanjian.name) and target.phase == Player.Play and
      not target:isKongcheng() and not target.dead and not player:isNude() and player:inMyAttackRange(target) and
      player:usedSkillTimes(yuanjian.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = yuanjian.name,
      prompt = "#lingling__yuanjian-invoke::"..target.id,
      cancelable = true,
      skip = true,
    })
    if #card > 0 then
      event:setCostData(self, {tos = {target}, cards = card})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, yuanjian.name, player, player)
    if player.dead or target.dead or target:isKongcheng() then return end
    local card = room:askToChooseCard(player, {
      target = target,
      flag = { card_data = {{ target.general, target:getCardIds("h") }} },
      skill_name = yuanjian.name,
      prompt = "#lingling__yuanjian-ask",
    })
    local mark = Fk:getCardById(card):getTableMark("lingling__yuanjian-inhand")
    table.insertIfNeed(mark, player.id)
    room:setCardMark(Fk:getCardById(card), "lingling__yuanjian-inhand", mark)
  end,
})

local spec = {
  can_refresh = function (self, event, target, player, data)
    return target == player and
      table.find(Card:getIdList(data.card), function (id)
        return Fk:getCardById(id):getMark("lingling__yuanjian-inhand") ~= 0
      end)
  end,
  on_refresh = function (self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.lingling__yuanjian = data.extra_data.lingling__yuanjian or {}
    for _, id in ipairs(Card:getIdList(data.card)) do
      table.insertTableIfNeed(data.extra_data.lingling__yuanjian, Fk:getCardById(id):getMark("lingling__yuanjian-inhand"))
    end
  end,
}
yuanjian:addEffect(fk.PreCardUse, spec)
yuanjian:addEffect(fk.PreCardRespond, spec)

local spec2 = {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return not player.dead and data.extra_data and data.extra_data.lingling__yuanjian and
      table.contains(data.extra_data.lingling__yuanjian, player.id)
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(2, yuanjian.name)
    if not target.dead then
      player.room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = yuanjian.name,
      }
    end
  end,
}
yuanjian:addEffect(fk.CardUseFinished, spec2)
yuanjian:addEffect(fk.CardRespondFinished, spec2)

return yuanjian
